import pygame
import gameClass
import loading
import random
import sys



# 游戏主循环
running = True
paused = False
clock = pygame.time.Clock()
enemy2_spawn_rate = 1  # 初始敌人2号的数量上限
last_spawn_time = pygame.time.get_ticks()
score = 0
def spawn_enemy1():
    if len(gameClass.enemies1) < 5:
        enemy1 = gameClass.Enemy1()
        gameClass.enemies1.add(enemy1)
        gameClass.all_sprites.add(enemy1)

def spawn_enemy2():
    if len(gameClass.enemies2) < enemy2_spawn_rate:
        enemy2 = gameClass.Enemy2()
        gameClass.enemies2.add(enemy2)
        gameClass.all_sprites.add(enemy2)

def spawn_enemy3():
    enemy3 = gameClass.Enemy3(loading.enemy3_image, loading.attack_image)
    gameClass.all_sprites.add(enemy3)
    gameClass.enemies3.add(enemy3)
def draw_score():
    score_text = loading.font.render(f"Score: {score}", True, (255, 255, 255))
    loading.screen.blit(score_text, (10, 10))
def spawn_prop1():
    prop1 = gameClass.Prop(loading.dz1_img)
    gameClass.props_group.add(prop1)
    gameClass.all_sprites.add(prop1)
    print(1)
def main_menu():
    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()
            elif event.type == pygame.MOUSEBUTTONDOWN:
                mouse_pos = pygame.mouse.get_pos()
                loading.an_s.play()
                if start_button_rect.collidepoint(mouse_pos):
                    return  # 点击开始按钮，退出主菜单
            elif event.type == pygame.KEYDOWN:
                loading.an_s.play()
                if event.key==pygame.K_SPACE:
                    return  # 点击开始按钮，退出主菜单
        # 绘制主菜单背景和按钮
        loading.screen.blit( loading.background_img, (0, 0))
        loading.screen.blit( loading.start_button_image, start_button_rect)
        pygame.display.flip()
        clock.tick(60)

# 设置开始按钮的位置
start_button_rect =  loading.start_button_image.get_rect(center=( loading.SCREEN_WIDTH // 2,  loading.SCREEN_HEIGHT // 2))

gameClass.play_video_in_game_loop()
# 显示主菜单
main_menu()

while running:
    draw_score()  # 确保 draw_score 在所有绘制操作之前被调用
    if not paused:
        # 控制帧率
        clock.tick(60)
        loading.BGM.play(loops=-1)
        now = pygame.time.get_ticks()
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False
            elif event.type == pygame.KEYDOWN:
                if event.key == pygame.K_SPACE:
                    loading.hq_s.play()
                    fireball = gameClass.Fireball(gameClass.player.rect.centerx + gameClass.player.rect.width // 2, gameClass.player.rect.centery)
                    gameClass.fireballs.add(fireball)
                    gameClass.all_sprites.add(fireball)
                elif event.key == pygame.K_ESCAPE:
                    loading.an_s.play()
                    paused = not paused  # Toggle the paused state
            elif event.type == pygame.MOUSEBUTTONDOWN:
                if paused and loading.pause_button_rect.collidepoint(event.pos):
                    loading.an_s.play()
                    paused = not paused


        # 生成敌人
        if random.randint(1, 100) <= 2:  # 调整概率
            spawn_enemy1()
        if random.randint(1, 100) <= 2:
            spawn_enemy2()
        if random.randint(1, 1000) <= 1:
            loading.sd1_s.play()
            loading.sd_s.play()
            lightning = gameClass.Lightning(loading.lightning1_image,loading.lightning2_image,-10,0)  # ()
            gameClass.all_sprites.add(lightning)
            gameClass.lightning_group.add(lightning)
        # 生成敌人3
        if now - last_spawn_time > 3000:  # 3秒生成一个敌人3
            loading.e3_s.play()
            enemy3 = gameClass.Enemy3(loading.enemy3_image)
            gameClass.all_sprites.add(enemy3)
            gameClass.enemies3.add(enemy3)
            last_spawn_time = now
        if random.randint(1, 1000) <= 2:
            spawn_prop1()
        # 更新所有精灵
        gameClass.all_sprites.update()
        gameClass.background.update()
        gameClass.timer.update()
        gameClass.lightning_group.update()

        # 检测角色和道具1之间的碰撞
        dz1 = pygame.sprite.groupcollide(gameClass.player_group, gameClass.props_group, False, True)
        if dz1:
            score += 100  # 每个道具1加100分
        # 检测火球和敌人1之间的碰撞
        collisions1 = pygame.sprite.groupcollide(gameClass.fireballs, gameClass.enemies1, True, True)
        if collisions1:
            loading.hit_s.play()
            gameClass.timer.add_time(2)  # 如果火球击中了敌人1，增加2秒时间
            score += 1  # 每个敌人加1分
        # 检测火球和敌人2之间的碰撞
        collisions2 = pygame.sprite.groupcollide(gameClass.fireballs, gameClass.enemies2, True, True)
        if collisions2:
            loading.hit_s.play()
            gameClass.timer.add_time(3)  # 如果火球击中了敌人2，增加3秒时间
            score += 1  # 每个敌人加1分
        # 检测炸弹和角色之间的碰撞
        if pygame.sprite.spritecollideany(gameClass.player, gameClass.bombs):
            gameClass.game_over_screen.draw(loading.screen)
            pygame.display.flip()
            pygame.time.wait(3000)  # 等待3秒钟
            break

        # 检测敌人3、敌人3攻击和角色之间的碰撞
        if pygame.sprite.spritecollideany(gameClass.player, gameClass.enemies3):
            gameClass.game_over_screen.draw(loading.screen)
            pygame.display.flip()
            pygame.time.wait(3000)  # 等待3秒钟
            break

        # 检测敌人和角色之间的碰撞
        if pygame.sprite.spritecollideany(gameClass.player, gameClass.enemies1) or pygame.sprite.spritecollideany(gameClass.player, gameClass.enemies2):
            gameClass.game_over_screen.draw(loading.screen)
            pygame.display.flip()
            pygame.time.wait(3000)  # 等待3秒钟
            break
            # 每隔一段时间（例如5秒）生成一组闪电
        # now = pygame.time.get_ticks()
        if now - last_spawn_time > 5000:
            lightning_x = random.randint(0, loading.SCREEN_WIDTH - loading.lightning1_image.get_width())
            lightning_y = 0
            lightning = gameClass.Lightning(loading.lightning1_image, loading.lightning2_image, lightning_x, lightning_y)
            gameClass.lightning_group.add(lightning)
            last_spawn_time = now
        # 检查玩家是否碰到了第二张闪电素材
        if pygame.sprite.spritecollide(gameClass.player, gameClass.lightning_group, False):
            for lightning in gameClass.lightning_group:
                if lightning.second_stage:
                    running = False
                    print("Game Over!")
                    break

        # 绘制
        gameClass.background.draw(loading.screen)
        gameClass.all_sprites.draw(loading.screen)
        gameClass.timer.draw(loading.screen)
        gameClass.lightning_group.draw(loading.screen)
        draw_score()  # 在所有绘制操作之后调用 draw_score

        # 如果时间耗尽，显示结算界面
        if gameClass.timer.current_time <= 0:
            gameClass.game_over_screen.draw(loading.screen)
            pygame.display.flip()
            pygame.time.wait(3000)  # 等待3秒钟
            break

        # 更新屏幕
        pygame.display.flip()

        # 每过半分钟增加敌人2号上限
        if pygame.time.get_ticks() // 30000 > enemy2_spawn_rate - 1:
            enemy2_spawn_rate += 1
    else:  # 注意：这里修改为 else 而不是 if paused
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False
            elif event.type == pygame.KEYDOWN:  # 添加 KEYDOWN 事件处理
                if event.key == pygame.K_ESCAPE:
                    loading.an_s.play()
                    paused = not paused  # Toggle the paused state
            elif event.type == pygame.MOUSEBUTTONDOWN:
                if paused and loading.pause_button_rect.collidepoint(event.pos):
                    loading.an_s.play()
                    paused = not paused
        loading.screen.blit(loading.pause_button_image, loading.pause_button_rect)
        pygame.display.flip()  # 在暂停状态下更新屏幕j


pygame.quit()
sys.exit()